/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Ltd. 		   */
/*																   */
/*	NAME	  = t__sqlserver_bigint_add.c						   */
/*	NOTE	  = 												   */
/*	DATE	  = 2014/11/25 by geyh								   */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>

#include "apmtest_cunit.h"
#include "fix_core.h"
#include "fix_comm.h"
#include "fix_fast.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

void case1(void)
{
    int i_ret = FR_SUCC;
    char *p_inbuff = (char *)frmalloc(100);
    char *p_inbuff_tmp = p_inbuff;
    unsigned int ui_size = 4;
    int i_presence = FIX_FAST_MANDATORY; //FIX_FAST_OPIONAL
    void *p_value = NULL;
    unsigned int ui_out = 0;
    memcpy(p_inbuff, "\x82\x39\x45\xa3", ui_size);
    Fix_fast_dec *p_st_dec = NULL;

    i_ret = fix_fast_decode_decimal(&p_inbuff, &ui_size
                                 , i_presence, &p_value, &ui_out);
    p_st_dec = (Fix_fast_dec *)p_value;

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_dec->i_exponential, 2);
    CU_ASSERT_EQUAL(p_st_dec->i_mantissa, 942755);
    CU_ASSERT_EQUAL(ui_out, sizeof(Fix_fast_dec));

    frfree(p_inbuff_tmp);
    frfree(p_value);
    return;
}

void case2(void)
{
    int i_ret = FR_SUCC;
    char *p_inbuff = (char *)frmalloc(100);
    char *p_inbuff_tmp = p_inbuff;
    unsigned int ui_size = 5;
    int i_presence = FIX_FAST_MANDATORY;
    void *p_value = NULL;
    unsigned int ui_out = 0;
    memcpy(p_inbuff, "\x81\x04\x3f\x34\xde", ui_size);
    Fix_fast_dec *p_st_dec = NULL;

    i_ret = fix_fast_decode_decimal(&p_inbuff, &ui_size
                                 , i_presence, &p_value, &ui_out);
    p_st_dec= (Fix_fast_dec *)p_value;

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_dec->i_exponential, 1);
    CU_ASSERT_EQUAL(p_st_dec->i_mantissa, 9427550);
    CU_ASSERT_EQUAL(ui_out, sizeof(Fix_fast_dec));

    frfree(p_inbuff_tmp);
    frfree(p_value);
    return;
}

void case3(void)
{
    int i_ret = FR_SUCC;
    char *p_inbuff = (char *)frmalloc(100);
    char *p_inbuff_tmp = p_inbuff;
    unsigned int ui_size = 4;
    int i_presence = FIX_FAST_OPIONAL; //
    void *p_value = NULL;
    unsigned int ui_out = 0;
    memcpy(p_inbuff, "\x83\x39\x45\xa3", ui_size);
    Fix_fast_dec *p_st_dec = NULL;

    i_ret = fix_fast_decode_decimal(&p_inbuff, &ui_size
                                 , i_presence, &p_value, &ui_out);
    p_st_dec = (Fix_fast_dec *)p_value;

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_dec->i_exponential, 2);
    CU_ASSERT_EQUAL(p_st_dec->i_mantissa, 942755);
    CU_ASSERT_EQUAL(ui_out, sizeof(Fix_fast_dec));

    frfree(p_inbuff_tmp);
    frfree(p_value);
    return;
}

void case4(void)
{
    int i_ret = FR_SUCC;
    char *p_inbuff = (char *)frmalloc(100);
    char *p_inbuff_tmp = p_inbuff;
    unsigned int ui_size = 4;
    int i_presence = FIX_FAST_MANDATORY;
    void *p_value = NULL;
    unsigned int ui_out = 0;
    memcpy(p_inbuff, "\xfe\x39\x45\xa3", ui_size);
    Fix_fast_dec *p_st_dec = NULL;

    i_ret = fix_fast_decode_decimal(&p_inbuff, &ui_size
                                 , i_presence, &p_value, &ui_out);
    p_st_dec= (Fix_fast_dec *)p_value;

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_dec->i_exponential, -2);
    CU_ASSERT_EQUAL(p_st_dec->i_mantissa, 942755);
    CU_ASSERT_EQUAL(ui_out, sizeof(Fix_fast_dec));

    frfree(p_inbuff_tmp);
    frfree(p_value);
    return;
}
void case5(void)
{
    int i_ret = FR_SUCC;
    char *p_inbuff = (char *)frmalloc(100);
    char *p_inbuff_tmp = p_inbuff;
    unsigned int ui_size = 4;
    int i_presence = FIX_FAST_OPIONAL; //
    void *p_value = NULL;
    unsigned int ui_out = 0;
    memcpy(p_inbuff, "\xfe\x46\x3a\xdd", ui_size);
    Fix_fast_dec *p_st_dec = NULL;

    i_ret = fix_fast_decode_decimal(&p_inbuff, &ui_size
                                 , i_presence, &p_value, &ui_out);
    p_st_dec = (Fix_fast_dec *)p_value;

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_dec->i_exponential, -2);
    CU_ASSERT_EQUAL(p_st_dec->i_mantissa, -942755);
    CU_ASSERT_EQUAL(ui_out, sizeof(Fix_fast_dec));

    frfree(p_inbuff_tmp);
    frfree(p_value);
    return;
}

void case6(void)
{
    int i_ret = FR_SUCC;
    char *p_inbuff = (char *)frmalloc(100);
    char *p_inbuff_tmp = p_inbuff;
    unsigned int ui_size = 4;
    int i_presence = FIX_FAST_OPIONAL; //
    void *p_value = NULL;
    unsigned int ui_out = 0;
    memcpy(p_inbuff, "\xfe\x39\x45\xa3", ui_size);
    Fix_fast_dec *p_st_dec = NULL;

    i_ret = fix_fast_decode_decimal(&p_inbuff, &ui_size
                                 , i_presence, &p_value, &ui_out);
    p_st_dec = (Fix_fast_dec *)p_value;

    CU_ASSERT_EQUAL(i_ret, FR_SUCC);
    CU_ASSERT_EQUAL(p_st_dec->i_exponential, -2);
    CU_ASSERT_EQUAL(p_st_dec->i_mantissa, 942755);
    CU_ASSERT_EQUAL(ui_out, sizeof(Fix_fast_dec));

    frfree(p_inbuff_tmp);
    frfree(p_value);
    return;
}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("fix_get_key_value", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}

	//增加测试用例
   	CU_add_test(pSuite, "case1", case1);
   	CU_add_test(pSuite, "case2", case2);
   	CU_add_test(pSuite, "case3", case3);
   	CU_add_test(pSuite, "case4", case4);
   	CU_add_test(pSuite, "case5", case5);
   	CU_add_test(pSuite, "case6", case6);

   	return 0;

}

int main(int argc, char **argv)
{
	fr_slab_init(102400, "ut_test");
	run_cunit(add_suite);
	//frshow_leak();
	fr_slab_destroy();
	return 0;
}

